#!/bin/bash
# Generate Cordova splash screens

function usage() {
	echo "usage: $0 <image-filename> <background-color> [output-directory]";
	exit 1;
}

[ "$1" ] && [ "$2" ] || usage
[ "$3" ] || set "$1" "$2" "."

image=$1
bgcolor=$2
output=$3/resources/splash

devices=ios,android #,windows-phone,bada,bada-wac,blackberry,webos
eval mkdir -p "$output/{$devices}"

# Show the user some progress by outputing all commands being run.
set -x

convert="convert $1 -background $bgcolor -gravity center"

function androidConvertSplash() {
	drawables=drawable-land-ldpi,drawable-land-mdpi,drawable-land-hdpi,drawable-land-xhdpi,drawable-port-ldpi,drawable-port-mdpi,drawable-port-hdpi,drawable-port-xhdpi
	eval mkdir -p "$output/android/{$drawables}"

 	$convert -resize 512x512 -extent 720x1280 "$output/android/drawable-port-xhdpi/screen.png"
 	$convert -resize 256x256 -extent 480x800 "$output/android/drawable-port-hdpi/screen.png"
 	$convert -resize 256x256 -extent 320x480 "$output/android/drawable-port-mdpi/screen.png"
 	$convert -resize 128x128 -extent 200x320 "$output/android/drawable-port-ldpi/screen.png"

 	$convert -resize 512x512 -extent 1280x720 "$output/android/drawable-land-xhdpi/screen.png"
 	$convert -resize 256x256 -extent 800x480 "$output/android/drawable-land-hdpi/screen.png"
 	$convert -resize 256x256 -extent 480x320 "$output/android/drawable-land-mdpi/screen.png"
 	$convert -resize 128x128 -extent 320x200 "$output/android/drawable-land-ldpi/screen.png"

	function androidCopySplash() {
		androidOutput=$(find . -type d | grep "platforms/android/res$")
		find . -type d | grep "resources/splash/android/drawable" | xargs -I{} cp -R {} $androidOutput
	}
	androidCopySplash
}
androidConvertSplash

function iosConvertSplash() {
	$convert -resize 512x512 -extent 640x1136 "$output/ios/Default-568h@2x~iphone.png"
	$convert -resize 1024x1024 -extent 2048x1496 "$output/ios/Default-Landscape@2x~ipad.png"
	$convert -resize 512x512 -extent 1024x748 "$output/ios/Default-Landscape~ipad.png"
	$convert -resize 1024x1024 -extent 1536x2008 "$output/ios/Default-Portrait@2x~ipad.png"
	$convert -resize 512x512 -extent 768x1004 "$output/ios/Default-Portrait~ipad.png"
	$convert -resize 256x256 -extent 320x480 "$output/ios/Default~iphone.png"
	$convert -resize 256x256 -extent 640x960 "$output/ios/Default@2x~iphone.png"

	function iosCopySplash() {
		cp $(find . | grep "resources/splash/ios/.*\.png") "$(find . -type d | grep 'platforms/ios/.*/Resources/splash')"
	}
	iosCopySplash
}
iosConvertSplash
